<template>
  <div class="app-container">
    <el-card shadow="never">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="耗材名称">
          <el-input v-model="searchForm.materialName" clearable> </el-input>
        </el-form-item>
        <el-form-item label="耗材单位">
          <el-select v-model="searchForm.unitName" placeholder="请选择">
            <el-option
              v-for="item in [
                '包',
                '桶',
                '袋',
                '盒',
                '箱',
                '个',
                '瓶',
                '片',
                '根',
                '双',
                '卷',
                '支',
                '把',
                '套',
                '件',
              ]"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleFilter" class="search-button"
            >查询</el-button
          >
          <el-button type="primary" @click="showAdd" class="add-button"
            >添加</el-button
          >
          <el-button @click="handleReset" class="search-button">重置</el-button>
          <el-button @click="goBack">返回</el-button>
        </el-form-item>
      </el-form>
      <el-table
        v-loading="listLoading"
        :data="list"
        element-loading-text="Loading"
        fit
        highlight-current-row
      >
        <el-table-column align="center" type="index" label="序号" width="55">
        </el-table-column>
        <el-table-column label="耗材名称" align="center" prop="materialName">
        </el-table-column>
        <el-table-column label="耗材图片" align="center" prop="materialPic">
          <template slot-scope="{ row }">
            <el-image
              style="width: 100px; height: 100px"
              :src="row.materialPic"
              :preview-src-list="[row.materialPic]"
            >
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="耗材单位" align="center" prop="unitName">
        </el-table-column>
        <!-- <el-table-column label="单价（元）" align="center" prop="price">
        </el-table-column> -->
        <el-table-column label="数量" align="center" prop="number">
        </el-table-column>
        <el-table-column label="规格" align="center" prop="specs">
        </el-table-column>
        <el-table-column align="center" label="操作" width="200">
          <template slot-scope="{ row }">
            <!-- <el-button @click="showEdit(row)" type="text">编辑</el-button> -->
            <el-button @click="delHandle(row)" type="text">移除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="searchForm.pageIndex"
        :limit.sync="searchForm.pageSize"
        @pagination="getList"
      />
    </el-card>
    <el-dialog :title="dialogTit" :visible.sync="dialogVisible" width="80%">
      <material v-if="dialogVisible" @close="close"></material>
    </el-dialog>
    <!-- 编辑 -->
    <el-dialog :title="editTitle" :visible.sync="editDialog" width="40%">
      <el-form
        label-position="right"
        label-width="120px"
        ref="form"
        :model="editForm"
      >
        <el-form-item label="耗材名称">
          <el-input v-model.trim="editForm.materialName"></el-input>
        </el-form-item>
        <el-form-item label="耗材图片">
          <uploadImg
            v-if="editDialog"
            v-model="editForm.materialPic"
          ></uploadImg>
        </el-form-item>
        <el-form-item label="耗材单位">
          <el-select v-model="editForm.unitName" placeholder="请选择">
            <el-option
              v-for="item in [
                '包',
                '桶',
                '袋',
                '盒',
                '箱',
                '个',
                '瓶',
                '片',
                '根',
                '双',
                '卷',
                '支',
                '把',
              ]"
              :key="item"
              :label="item"
              :value="item"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="单价（元）">
          <el-input v-model.trim="editForm.price"></el-input>
        </el-form-item>
        <el-form-item label="规格">
          <el-input v-model.trim="editForm.specs"></el-input>
        </el-form-item>
        <el-form-item label="数量">
          <el-input-number v-model.trim="editForm.number"></el-input-number>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialog = false" class="btn-cancel"
          >取 消</el-button
        >
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import * as consumable from '@/api/consumable'
import Pagination from '@/components/Pagination'
import material from '@/components/consumableHospital.vue'
import uploadImg from '../../components/uploadImg.vue'
export default {
  components: { Pagination, material, uploadImg },
  data() {
    return {
      list: null,
      listLoading: true,
      searchForm: {
        pageIndex: 1,
        pageSize: 10
      },
      total: 0,
      dialogVisible: false,
      dialogTit: '',
      form: {},
      selectionData: [],
      editDialog: false,
      editTitle: '',
      editForm: {},
    }
  },
  created() {
    this.searchForm.medicalId = this.$route.query.uid
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      let param = Object.assign({}, this.searchForm)
      param.hospitalUid = localStorage.getItem('hospitalUid')
      consumable.queryData(param).then(response => {
        this.list = response.data.rows
        this.total = response.data.total
        this.listLoading = false
      }).catch(error => {
        this.listLoading = false
        console.log(error)
      })
    },
    handleFilter() {
      this.searchForm.pageIndex = 1
      this.getList()
    },
    handleReset() {
      this.resetTemp();
      this.getList();
    },
    resetTemp() {
      this.searchForm = {
        pageIndex: 1,
        pageSize: 10
      }
    },
    showAdd() {
      this.dialogTit = '新增'
      this.form = {}
      this.dialogVisible = true
    },
    showEdit(row) {
      this.editTitle = '编辑'
      this.editForm = Object.assign({}, row)
      delete this.editForm.createTime
      delete this.editForm.updateTime
      this.editDialog = true
    },
    submit() {
      if (this.editTitle === '新增') {
        this.creatHandle()
      } else if (this.editTitle === '编辑') {
        this.editHandle()
      }
    },
    creatHandle() {
      consumable.insert(this.form).then(response => {
        if (response.code == 200) {
          this.dialogVisible = false
          this.getList()
          this.$message.success('新增成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    editHandle() {
      consumable.edit(this.editForm).then(response => {
        if (response.code == 200) {
          this.editDialog = false
          this.getList()
          this.$message.success('修改成功')
        }
      }).catch(error => {
        console.log(error)
      })
    },
    delHandle(data) {
      this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let param = {
          uid: data.uid,
        }
        consumable.del(param).then(result => {
          this.$message.success(result.msg)
          this.getList()
        }).catch(error => {
          this.loading = false
          console.log(error)
        })
      }).catch(() => {
        console.log('取消删除')
      });
    },
    close(msg) {
      if (msg === '') {
        this.dialogVisible = false
      } else {
        let batchUses = msg
        batchUses.map(item => {
          delete item.createTime
          delete item.updateTime
          item.materialUseId = item.uid
          item.medicalId = this.$route.query.uid
          item.hospitalUid = localStorage.getItem('hospitalUid')
          item.hospitalName = localStorage.getItem('hospitalAme')
          delete item.uid
        })

        consumable.insertBatch(batchUses).then(response => {
          if (response.code == 200) {
            this.dialogVisible = false
            this.getList()
            this.$message.success('新增成功')
          }
        }).catch(error => {
          this.$message.error(error)
          console.log(error)
        })
      }
    },
    goBack() {
      this.$router.go(-1)
    }
  }
}
</script>
